package com.ljm.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljm.entity.UserEntity;
import com.ljm.entity.UserEntityVo;
import com.ljm.entity.vo.pathVo;
import com.ljm.security.TokenManager;
import com.ljm.service.*;
import com.ljm.utils.R;
import com.ljm.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author song
 * @since 2021-05-18
 */
@RestController
@RequestMapping("/userEntity")
public class UserController {

    @Autowired
    TokenManager tokenManager;

    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    PieceDetailService pieceDetailService;

    @Autowired
    JurisdictionService jurisdictionService;


//   @PreAuthorize("hasRole('svip')")
    @RequestMapping("/List")
    public R List(){
        return R.ok().message("当前执行 用户的 列表方法");
    }

    @RequestMapping("/Add")
    @PreAuthorize("hasAuthority('ure:user:add')")
    public R add(){
        return R.ok().message("当前执行 用户的 新增方法");
    }
    @RequestMapping("/Updata")
    public R uopdata(){
        return R.ok().message("当前执行 用户的 修改方法");
    }
    @RequestMapping("/Delete")
    @PreAuthorize("hasAuthority('ure:user:list')")
    public R Delete(){
        return R.ok().message("当前执行 用户的 删除方法");
    }

    @RequestMapping("/info/{accessToken}") //String token
    public R info(@PathVariable  String accessToken){
        System.out.println("accessToken  +  "+accessToken);
        //获取当前登录用户用户名
        String userFromToken = tokenManager.getUserFromToken(accessToken);
        //String userFromToken = SecurityContextHolder.getContext().getAuthentication().getName();
        //Map<String, Object> userInfo = indexService.getUserInfo(username);
        return R.ok().message(userFromToken);
    }
    /* 返回用户名*/
    @RequestMapping("/info") //String token
    public R infos(){
        //System.out.println("accessToken  +  "+accessToken);
        //获取当前登录用户用户名
        //String userFromToken = tokenManager.getUserFromToken(accessToken);
        String userFromToken = SecurityContextHolder.getContext().getAuthentication().getName();
        //Map<String, Object> userInfo = indexService.getUserInfo(username);
        return R.ok().message(userFromToken);
    }
    /*  用户名查询到 用户可以查看菜单 */
    @RequestMapping("/menc") //String token
    public R menc(){
        //获取当前登录用户用户名
        //String userFromToken = tokenManager.getUserFromToken(accessToken);
        String userFromToken = SecurityContextHolder.getContext().getAuthentication().getName();
        UserEntity user_login = userService.getOne(new QueryWrapper<UserEntity>().eq("user_login", userFromToken));
        List<pathVo> list=userService.getbyname(user_login.getUserid());

        return R.ok().data("list",list);
    }


    @RequestMapping("/getbyID1")
    public R getbyID1(HttpServletRequest req, HttpServletResponse res){
        QueryWrapper<UserEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("userid",1);
        System.out.println("执行  getall"+userService.list(objectQueryWrapper));
        String userName = userService.list(objectQueryWrapper).get(0).getUserName();
        //chain.doFilter(req,res);
        Cookie cookie = new Cookie("cookiename",userName);
        res.addCookie(cookie);
        //ResponseUtil.out(res, R.ok().setObject(userName));
        return R.ok().message(userName);
    }
    @RequestMapping("/getbyname")
    public R getbyname(HttpServletRequest req, HttpServletResponse res){
        return R.ok().message(userService.list(null).toString());
    }
    @RequestMapping("/getby")
    public List<UserEntityVo> getby(String name){

        return null;
    }

    /* 依据 用户查询用户角色 + 权限 */
    @RequestMapping("/menu/nav")
    public  R mencnav(){
        String userFromToken = SecurityContextHolder.getContext().getAuthentication().getName();
        UserEntity user_login = userService.getOne(new QueryWrapper<UserEntity>().eq("user_login", userFromToken));
        Integer userid = user_login.getUserid();
        /// 权限
        String str=userService.getUserAuthorityMen(userFromToken);
        System.out.println("str "+str);
        String[] authorityInfoArray = StringUtils.tokenizeToStringArray(str, ",");
        /// 导航
        List<pathVo> getbyname = userService.getbyname(userid);
        for (pathVo pathVo:getbyname) {
            pathVo.setTitle(pathVo.getPiecename());
        }
          return R.ok().data("authoritys",authorityInfoArray).data("nav",getbyname);
//        return R.ok(MapUtil.builder()
//                .put("authoritys", authorityInfoArray)
//                .put("nav", getbyname)
//                .map()
//        );
    }

    @PostMapping("logout")
    public R logout(){
        return R.ok();
    }

}

